const initVersion = require('./api-version-init')
const initModuleTree = require('./api-module-tree-init')
const initBtn = require('./api-button-init')
const initForm = require('./api-form-init')
const initRequestParameter = require('./api-request-parameter-init')
const initResponseParameter = require('./api-response-parameter-init')
const initApiTest = require('./api-test-init')
const initTemplate = require('./api-template-init')
const initBug = require('./api-bug-init')
const initExport = require('./api-export-init')
const initParameter = require('./api-parameter-init')

const initKey = require('./api-key-init')

const {
	loadProjectData,
} = require('./api-data-2x')
const {
	$U_SetItem,
	$U_GetItem,
} = require('@utils/storage')

/**
 * 初始化接口编辑页面
 * 只有在isLock = 1情况下接口才可编辑
 * 所有状态信息都存储在apiComp.store中
 * @param  {[type]} projectId [description]
 * @return {[type]}           [description]
 */
module.exports = async function initAPIPage(projectId, exists) {
	let {
		apiComp,
	} = $['components-jq']
	if (!exists) {
		apiComp.layoutComp = $("#api-layout")
		apiComp.treeComp = $("#api-tree")
		apiComp.treeMenuComp = $("#api-tree-tool-menu")
		apiComp.btnEdit = $("#api-btn-edit")
		apiComp.btnCancel = $("#api-btn-cancel")
		apiComp.btnSave = $("#api-btn-save")
		apiComp.btnQuickSave = $("#api-btn-quick-save")
		apiComp.btnVersion = $("#api-btn-version")
		apiComp.btnVersionBack = $("#api-btn-version-back")
		apiComp.testServerUrlComp = $("#api-test-server-url")
		apiComp.reqParamsComp = $("#api-req")
		apiComp.resParamsComp = $("#api-res")
		apiComp.reqParamsMenu = $("#req-params-menu")
		apiComp.resParamsMenu = $("#res-params-menu")
		apiComp.parameterDialog = $("#api-parameter-dialog")
		apiComp.parameterForm = $("#api-parameter-form")
		apiComp.parameterName = $("#api-parameter-name")
		apiComp.parameterMandatory = $("#api-parameter-mandatory")
		apiComp.parameterType = $("#api-parameter-type")
		apiComp.parameterContent = $("#api-parameter-content")
		apiComp.parameterRemark = $("#api-parameter-remark")
		apiComp.formComp = $("#api-form");
		apiComp.formId = $("#api-form-id")
		apiComp.formName = $("#api-form-name")
		apiComp.formContentType = $("#api-form-content-type")
		apiComp.formPath = $("#api-form-path")
		apiComp.formMethod = $("#api-form-method")
		apiComp.formIntro = $("#api-form-intro")
		apiComp.jsonDialog = $("#api-json-dialog")
		apiComp.jsonText = $("#api-json-text")
		apiComp.versionDialog = $("#api-version-dialog")
		apiComp.versionTable = $("#api-version-table")
		apiComp.versionHistoryDialog = $("#api-version-history-dialog")
		apiComp.saveDialog = $("#api-save-dialog")
		apiComp.saveFormVersion = $("#api-save-form-version")
		apiComp.saveFormIntro = $("#api-save-form-intro")
		apiComp.templateDialog = $("#api-template-dialog")
		apiComp.templateTree = $("#api-template-tree")
		apiComp.templateTable = $("#api-template-table")
		apiComp.testHeaderDialog = $("#api-test-header-dialog")
		apiComp.testHeaderForm = $("#api-test-header-form")
		apiComp.testHeaderText = $("#api-test-header-text")
		apiComp.alertDialog = $("#api-alert-dialog")
		apiComp.alertText = $("#api-alert-text")
		apiComp.bugBtn = $("#api-bug-button")
		apiComp.bugDialog = $("#api-bug-dialog")
		apiComp.bugText = $("#api-bug-text")
		apiComp.exportBtn = $("#api-export-button")
		initModuleTree(apiComp)
		initBtn(apiComp)
		initForm(apiComp)
		initRequestParameter(apiComp)
		initResponseParameter(apiComp)
		initParameter(apiComp)
		initVersion(apiComp, projectId)
		initApiTest(apiComp)
		initTemplate(apiComp)
		initBug(apiComp)
		initExport(apiComp)
		initKey(apiComp)
	}
	await loadProjectData(apiComp, projectId)
}